﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lucilla.Framework.Web.Builders.SlickGrid;
using Newtonsoft.Json;

namespace Lucilla.Playground.Website.Areas.Controls.Models.SlickGrid
{
    public class SampleGrid1Model
    {
        public string ColumnsSource { get; private set; }
        private IList<Grid1RowModel> _rows;


        public SampleGrid1Model()
        {
            _rows = new List<Grid1RowModel>();
            BuildGrid();
        }

        public void AddRow(Grid1RowModel row)
        {
            _rows.Add(row);
        }

        private void BuildGrid()
        {
            SlickGridBuilder builder = new SlickGridBuilder();

            builder.AddColumn(Column
                          .ForId("Row")
                          .Width(40)
                          .WithEditorFunction("TextCellEditor")
                          .WithSetValueHandler("gridUpdateItem")
                          .CannotTriggerInsert()
                          .Unselectable()
                          .CssClass("cell-number")
                ).AddColumn(Column
                                .ForId("ItemId")
                                .Named("Item")
                                .WithEditorFunction("TextCellEditor")
                                .WithSetValueHandler("gridUpdateItem")
                ).AddColumn(Column
                                .ForId("Description")
                                .ForField("ItemDescription")
                                .Width(180)
                                .WithEditorFunction("TextCellEditor")
                                .WithSetValueHandler("gridUpdateItem")
                ).AddColumn(Column
                                .ForId("Price")
                                .Configure(GridConfigurator.AsMoney)
                                .WithSetValueHandler("gridUpdateItem")
                ).AddColumn(Column
                                .ForId("VAT")
                                .Configure(GridConfigurator.AsMoney)
                                .WithSetValueHandler("gridUpdateItem")
                ).AddColumn(Column
                                .ForId("Total")
                                .Configure(GridConfigurator.AsMoney)
                                .WithSetValueHandler("gridUpdateItem")
                );


            ColumnsSource = builder.GetColumnsSource();
        }

        public string GridData
        {
            get
            {
                return JsonConvert.SerializeObject(_rows);
            }
        }
    }
}